package cn.hgd11.security.service.impl;

import cn.hgd11.security.common.CommonResult;
import cn.hgd11.security.entity.SysUser;
import cn.hgd11.security.mapper.SysUserMapper;
import cn.hgd11.security.service.SysUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 用户信息表 服务实现类
 * </p>
 *
 * @author kaige.wang
 * @since 2022-03-10
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService, UserDetailsService {

    @Override
    public CommonResult<Integer> createUser(SysUser user) {
        this.baseMapper.insert(user);
        return CommonResult.success(user.getId());
    }

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        SysUser sysUser = this.baseMapper.selectByUsername(username);

        if (sysUser == null) {
            throw  new UsernameNotFoundException("用户名未找到");
        }
        return sysUser;
    }
}
